package com.weitu.contractor.service.impl;

import com.weitu.contractor.entity.ContractorInfo;
import com.weitu.contractor.entity.ContractorInfoCriteria;
import com.weitu.contractor.mapper.ContractorInfoMapper;
import com.weitu.contractor.service.ContractorInfoService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class ContractorInfoServiceImpl implements ContractorInfoService {
    private final ContractorInfoMapper contractorInfoMapper;

    private static final Logger logger = LoggerFactory.getLogger(ContractorInfoServiceImpl.class);

    @Autowired
    public ContractorInfoServiceImpl(ContractorInfoMapper contractorInfoMapper) {
        this.contractorInfoMapper = contractorInfoMapper;
    }

    @Override
    public int countByCriteria(ContractorInfoCriteria example) {
        int count = this.contractorInfoMapper.countByCriteria(example);
        logger.debug("count: {}", count);
        return count;
    }

    @Override
    public ContractorInfo selectById(String id) {
        return this.contractorInfoMapper.selectById(id);
    }

    @Override
    public List<ContractorInfo> selectByCriteria(ContractorInfoCriteria example) {
        return this.contractorInfoMapper.selectByCriteria(example);
    }

    @Override
    @Transactional
    public int deleteById(String id) {
        return this.contractorInfoMapper.deleteById(id);
    }

    @Override
    @Transactional
    public boolean updateByIdSelective(ContractorInfo record) {
        return this.contractorInfoMapper.updateByIdSelective(record) > 0;
    }

    @Override
    @Transactional
    public int deleteByCriteria(ContractorInfoCriteria example) {
        return this.contractorInfoMapper.deleteByCriteria(example);
    }

    @Override
    @Transactional
    public int updateByCriteriaSelective(ContractorInfo record, ContractorInfoCriteria example) {
        return this.contractorInfoMapper.updateByCriteriaSelective(record, example);
    }

    @Override
    @Transactional
    public int insertSelective(ContractorInfo record) {
        return this.contractorInfoMapper.insertSelective(record);
    }

    @Override
    public List<ContractorInfo> queryList(Map paramMap) {
        return this.contractorInfoMapper.queryList(paramMap);
    }

    @Override
    public int queryCount(Map paramMap) {
        return this.contractorInfoMapper.queryCount(paramMap);
    }

    @Override
    public boolean checkIfExist(String name, String code) {
        ContractorInfoCriteria criteria = new ContractorInfoCriteria();
        criteria.or().andNameEqualTo(name).andDelStateEqualTo(0);
        criteria.or().andCodeEqualTo(code).andDelStateEqualTo(0);
        List<ContractorInfo> contractorInfoList = this.contractorInfoMapper.selectByCriteria(criteria);
        return contractorInfoList.size() > 0;
    }

    @Override
    @Transactional
    public boolean insertIfNotExist(ContractorInfo contractorInfo) {
        return this.contractorInfoMapper.insertSelective(contractorInfo) > 0;
    }

    @Override
    public List<String> selectContractorIdsByName(String name) {
        List<String> contractorIds = new ArrayList<String>();
        ContractorInfoCriteria cr = new ContractorInfoCriteria();
        cr.createCriteria().andNameLike("%" + name + "%");
        List<ContractorInfo> contractorInfoList = this.selectByCriteria(cr);
        if (contractorInfoList.isEmpty()) {
            return contractorIds;
        }
        for (ContractorInfo contractorInfo : contractorInfoList) {
            contractorIds.add(contractorInfo.getId());
        }
        return contractorIds;
    }
}